package com.web.kdl.config.security.components;

import cn.hutool.json.JSONUtil;
import com.web.kdl.common.result.CommonResult;
import com.web.kdl.common.result.ReturnCode;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author： shulibin
 * @Date： 2025/7/25 10:52
 * @Describe：没有登录的处理类
 */
public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        // 响应 401 暂未登录或token已过期
        response.getWriter().println(JSONUtil.parse(CommonResult.error(ReturnCode.UNAUTHORIZED.getCode())));
        response.getWriter().flush();
    }
}
